package org.ubc.cs410.disposal2020;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/*
 * Methods associated to the Player Class
 */

public class PlayerFactory {
	public static Player getPlayer(String user) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(Player.class);
		query.setFilter("user == userParam && active == true");
		query.declareParameters("String userParam");

		try {
			List<Player> results = (List<Player>) query.execute(user);
			if (!results.isEmpty()) {
				return results.get(0);
			} else {
				return null;
			}
		} finally {
			query.closeAll();
		}
	}

	public static Player getCurrentPlayer() {
		return getPlayer(UserServiceFactory.getUserService().getCurrentUser().getEmail());
	}

}
